import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import React from 'react';
import { Icon, Tooltip } from 'antd';
import { withRouter } from 'dva/router';
import { connect } from 'dva';
import intl from "../../../utils/intl";
import { openTab } from "../../../utils/menuTab";
import notification from "../../../utils/notification";
import { setSession } from "../../../utils/utils";
import { loadTraceLogAsync, watchTraceLogSetComponent } from "../../../customize/traceLog";

var TraceComp = function TraceComp(props) {
  var _props$onTrace = props.onTrace,
      onTrace = _props$onTrace === void 0 ? function (e) {
    return e;
  } : _props$onTrace,
      hasCims = props.hasCims,
      isStart = props.isStart;
  return hasCims ? /*#__PURE__*/React.createElement(Tooltip, {
    title: !isStart ? intl.get('cadm.traceLog.view.title.start').d('启动日志追溯分析') : intl.get('cadm.traceLog.view.title.end').d('结束日志追溯分析')
  }, /*#__PURE__*/React.createElement(Icon, {
    type: isStart ? 'pause-circle-o' : 'play-circle-o',
    onClick: onTrace,
    style: {
      color: isStart ? '#ff0000' : '#fff',
      fontSize: '18px',
      cursor: 'pointer',
      lineHeight: 1,
      marginRight: '12px'
    }
  })) : null;
};

var DefaultTraceLog = /*#__PURE__*/function (_React$Component) {
  _inherits(DefaultTraceLog, _React$Component);

  var _super = _createSuper(DefaultTraceLog);

  function DefaultTraceLog() {
    var _this;

    _classCallCheck(this, DefaultTraceLog);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      hasCims: false
    };

    _this.handleTrace = function () {
      var dispatch = _this.props.dispatch;
      dispatch({
        type: 'global/getTraceStatus'
      }).then(function (res) {
        if (res) {
          _this.handleTraceEnd();
        } else {
          _this.handleTraceStart();
        }
      });
    };

    _this.handleTraceStart = function () {
      var dispatch = _this.props.dispatch;
      dispatch({
        type: 'global/startTrace'
      }).then(function (res) {
        if (res) {
          // 启动成功后，此时trace状态为启动,并清除历史记录
          sessionStorage.removeItem('traceLog');
          dispatch({
            type: 'global/updateState',
            payload: {
              traceGroupId: undefined,
              traceStatus: true
            }
          });
          notification.success();
        }
      });
    };

    _this.handleTraceEnd = function () {
      var dispatch = _this.props.dispatch;
      dispatch({
        type: 'global/endTrace'
      }).then(function (res) {
        if (res) {
          var _res$traceList = res.traceList,
              traceList = _res$traceList === void 0 ? [] : _res$traceList,
              traceGroupId = res.traceGroupId; // 缓存数据到session

          setSession('traceLog', traceList);
          notification.success(); // 启动结束后，此时trace状态为false

          dispatch({
            type: 'global/updateState',
            payload: {
              traceGroupId: traceGroupId,
              traceStatus: false
            }
          }); // 清除  traceGroupId，5分钟后控制导出不可点击

          setTimeout(function () {
            dispatch({
              type: 'global/updateState',
              payload: {
                traceGroupId: undefined
              }
            });
          }, 300000);
          openTab({
            key: '/cadm/trace-log',
            path: '/cadm/trace-log/list',
            title: intl.get('cadm.traceLog.view.title').d('日志追溯分析')
          });
        }
      });
    };

    _this.importCard = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var loadCard;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              loadCard = null;
              _context.prev = 1;
              _context.next = 4;
              return loadTraceLogAsync('TraceLog');

            case 4:
              loadCard = _context.sent;
              _context.next = 10;
              break;

            case 7:
              _context.prev = 7;
              _context.t0 = _context["catch"](1);
              loadCard = null;

            case 10:
              return _context.abrupt("return", loadCard);

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[1, 7]]);
    }));
    return _this;
  }

  _createClass(DefaultTraceLog, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      this.unListener = watchTraceLogSetComponent(function () {
        _this2.importCard().then(function (res) {
          if (res) {
            _this2.setState({
              hasCims: true
            });
          } else {
            _this2.setState({
              hasCims: false
            });
          }
        });
      }, true);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.unListener) {
        this.unListener();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var traceStatus = this.props.traceStatus;
      var hasCims = this.state.hasCims;
      return /*#__PURE__*/React.createElement(TraceComp, {
        hasCims: hasCims,
        onTrace: this.handleTrace,
        isStart: traceStatus
      });
    }
  }]);

  return DefaultTraceLog;
}(React.Component);

export default withRouter(connect(function (_ref2) {
  var global = _ref2.global;
  return {
    traceStatus: global.traceStatus
  };
})(DefaultTraceLog));